<?php

use Biz\model\brandActivity\BlindBoxPointTask as mBlindBoxPointTask;
use Biz\service\brand\BlindBox;
use Doraemon\tools\PointConfig;
use Hhzbeads\Point\PointClient;
use Biz\cache\brand\BrandShowActivity;

/**
 * 领取任务积分（完成当日任务）
 */
class ReceiveTaskPointAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        if (!BrandShowActivity::isLock($this->uid . '-blind-box',2)) {
            $taskType = $this->getRequest()->getPost('task_type', 0);
            if (!$taskType && !in_array($taskType, mBlindBoxPointTask::TASK_TYPE_ARRAY)) {
                throw new \Exception('参数错误');
            }
            $this->receivePoint($this->uid, $taskType);
            $this->setSuccess();
        } else {
            throw new \Exception('你的操作太频繁');
        }
    }

    public function receivePoint($uid, $taskType)
    {
        $eventName = '';
        $point = 0;
        switch ($taskType) {
            case mBlindBoxPointTask::TASK_TYPE_SIGN_IN:
                $eventName = PointConfig::POINT_TYPE_BLIND_BOX_SIGN_IN;
                $point = BlindBox::TASK_SIGN_POINT;
                break;
            case mBlindBoxPointTask::TASK_TYPE_SHARE_GOODS:
                $eventName = PointConfig::POINT_TYPE_BLIND_BOX_SHARE_WIKI;
                $point = BlindBox::TASK_SHARE_GOODS_POINT;
                break;
            case mBlindBoxPointTask::TASK_TYPE_RELATE_WIKI:
                $eventName = PointConfig::POINT_TYPE_BLIND_BOX_RELATE_WIKI;
                $point = BlindBox::TASK_RELATE_WIKI_POINT;
                break;
            case mBlindBoxPointTask::TASK_TYPE_RELATE_TOPIC:
                $eventName = PointConfig::POINT_TYPE_BLIND_BOX_RELATE_TOPIC;
                $point = BlindBox::TASK_RELATE_TOPIC;
                break;
        }
        if ($eventName && $point) {
            $start = date('Y-m-d');
            $end = date('Y-m-d', strtotime($start) + 86399);
            $insertData = ['uid' => $uid, 'task_type' => $taskType, 'point' => $point, 'task_state' => mBlindBoxPointTask::TASK_STATE_FINISHED];
            if ($taskType == mBlindBoxPointTask::TASK_TYPE_RELATE_TOPIC) {
                $where = ['uid' => $uid, 'task_type' => $taskType];
                $insertData['obj_id'] = BlindBox::TASK_RELATE_TOPIC_ID;
                $request_id = $uid . '-' . BlindBox::TASK_RELATE_TOPIC_ID . '-' . $eventName;
            } else {
                $where = ['uid' => $uid, 'addtime[>=]' => $start, 'addtime[<=]' => $end, 'task_type' => $taskType];
                $request_id = $uid . '-' . date('Ymd') . '-' . $eventName;
            }
            $info = mBlindBoxPointTask::getByWhere($where);
            if ($info) {
                if ($info['task_state'] == mBlindBoxPointTask::TASK_STATE_FINISHED) {
                    throw new \Exception('今日已经领取过积分');
                }
                mBlindBoxPointTask::updateData(['task_state' => mBlindBoxPointTask::TASK_STATE_FINISHED], ['id' => $info['id']]);
            } else {
                mBlindBoxPointTask::insertData($insertData);
            }
            //加积分
            $request_id = $uid . '-' . date('Ymd') . '-' . $eventName;
            try {
                PointClient::awardPoint($uid, $point, $eventName, $request_id, 'community', json_encode(['date' => date('Y-m-d'), 'uid' => $uid]));
            } catch (\Exception $e) {
                \Doraemon\tools\Log::warning($e->getMessage(), [$e->getFile(), $e->getLine()]);
            }
        }
    }
}